import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index'
import Layout from '@/layout/index'
import {Storage} from "@/utils/storage"

Vue.use(VueRouter)

// 自动生成菜单
const menu = [
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('@/views/dashboard/index'),
    meta: {
      title: '主页',
      icon: 'ios-home-outline'
    }
  },
  {
    path: '/goods',
    name: 'Goods',
    meta: {
      title: '商品管理',
      icon: 'ios-book-outline'
    },
    component: () => import('@/layout/index'),
    children: [
      {
        path: 'goodsList',
        name: 'GoodsList',
        component: () => import('@/views/goods/list'),
        meta: {
          title: '商品查询'
        }
      }
    ]
  },
  {
    path: '/purchase',
    name: 'Purchase',
    meta: {
      title: '库存管理',
      icon: 'ios-archive-outline'
    },
    component: () => import('@/layout/index'),
    children: [
      {
        path: 'purchaseList',
        name: 'PurchaseList',
        component: () => import('@/views/purchase/list'),
        meta: {
          title: '库存查询'
        }
      },
      {
        path: 'apply',
        name: 'Apply',
        component: () => import('@/views/purchase/apply'),
        meta: {
          title: '进货申请'
        }
      }
    ]
  },
  {
    path: '/stocks',
    name: 'Stocks',
    meta: {
      title: '进货管理',
      icon: 'ios-appstore-outline'
    },
    component: () => import('@/layout/index'),
    children: [
      {
        path: 'review',
        name: 'Review',
        component: () => import('@/views/stocks/review'),
        meta: {
          title: '进货审批'
        }
      }
    ]
  }
]
const WriteList = ['/login']

function createTitle(menu) {
  let obj = {}
  menu.forEach(item => {
    obj[item.name.toLowerCase()] = item.meta.title
    if (item.children != null) {
      obj = {...obj, ...createTitle(item.children)}
    }
  })
  return obj
}
const titles = createTitle(menu)
Vue.prototype.$menu = menu

const routes = [
  {
    path: '/',
    component: Layout,
    redirect: 'dashboard',
    children: [{
      path: '/dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {
        title: '主页',
        icon: 'ios-home-outline'
      }
    }]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index')
  },
  {
    path: '/404',
    component: () => import('@/views/404')
  },
  ...menu.slice(1)
  // { path: '*', redirect: '/404' },
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  const token = Storage.getItem('TOKEN')
  if (token == null && !WriteList.includes(to.path)) {
    Vue.prototype.$Message.error('请重新登录')
    next({ path: '/login'} )
    return false
  } else {
    store.commit('changeRoute', to.fullPath.split('/').slice(1).map(key => titles[key.toLowerCase()]))
    next()
  }
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
